<template>
  <div class="app-container">
    <!--顶部搜索表单-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="机构名称" prop="checkOrgName">
        <el-input
          v-model="queryParams.checkOrgName"
          placeholder="请输入检测机构名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构编码" prop="checkOrgCode">
        <el-input
          v-model="queryParams.checkOrgCode"
          placeholder="请输入检测机构编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入采样机构联系人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" v-if="advanced">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <a @click="toggleAdvanced" style="margin-left: 8px">
          {{ advanced ? '收起' : '展开' }}
          <i v-if="!advanced" class="el-icon-arrow-down"></i>
          <i v-if="advanced" class="el-icon-arrow-up"></i>
        </a>
      </el-form-item>
    </el-form>
    <!--表格顶部操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['subject:checkOrg:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据-->
    <el-table v-loading="loading" :data="checkOrgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机构名称" align="center" :show-overflow-tooltip="true" width="180">
        <template slot-scope="scope">
          <el-tag type="primary">{{scope.row.checkOrgName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="机构编码" align="center" prop="checkOrgCode" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="联系人" align="center" prop="contactName" width="100" />
      <el-table-column label="联系方式" align="center" prop="phone" width="120" />
      <el-table-column label="地址" align="center" prop="address" width="200" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseDateTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 底部分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采样机构对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="checkOrgName">
              <el-input v-model="form.checkOrgName" placeholder="请输入检测机构名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构编码" prop="checkOrgCode">
              <el-input v-model="form.checkOrgCode" placeholder="请输入检测机构编码" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入联系人" maxlength="11" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系方式" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入采样机构地址"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {addCheckOrg, deleteCheckOrg, getCheckOrg, listCheckOrg, updateCheckOrg} from "@/api/subject/checkOrg/checkOrg";

export default {
  name: "index",
  data(){
    return {
      showSearch: true,
      dateRange: [],
      // 高级搜索 展开/关闭
      advanced: false,
      single: true,
      multiple: true,
      loading: false,
      total: 0,
      title: '',
      open: false,
      form: {},
      checkOrgList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        checkOrgName: [
          { required: true, message: "采样机构名称不能为空", trigger: "blur" }
        ],
        checkOrgCode: [
          { required: true, message: "采样机构编码不能为空", trigger: "blur" }
        ],
        contactName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList(){
      this.loading = true;
      console.log(this.queryParams)
      listCheckOrg(this.buildQueryTime(this.queryParams, this.dateRange)).then(response => {
          this.checkOrgList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleQuery(){
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    resetQuery(){

    },
    toggleAdvanced(){
      this.advanced = !this.advanced;
    },
    reset(){
      this.form = {
        checkOrgName: undefined,
        checkOrgCode: undefined,
        contactName: undefined,
        phone: undefined,
        address: undefined
      };
      this.resetForm("form");
    },
    handleAdd(){
      this.reset();
      this.open = true;
      this.title = "添加检测机构";
    },
    handleUpdate(row){
      this.reset();
      const id = row.id || this.ids;
      getCheckOrg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测机构";
      });
    },
    handleDelete(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return deleteCheckOrg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport(){

    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateCheckOrg(this.form).then(response =>{
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            addCheckOrg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel(){
      this.open = false;
      this.reset();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
  }
}
</script>

<style scoped>

</style>
